package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSON;
import com.bdqn.tour.pojo.HzyLocation;
import com.bdqn.tour.service.HzyLocationService;
import com.bdqn.tour.service.impl.HzyLocationServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/addLocation")
public class AddLocationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        // 获取数据
        String receiverName = req.getParameter("receiverName");
        String province = req.getParameter("province");
        String city = req.getParameter("city");
        String district = req.getParameter("district");
        String streetAddress = req.getParameter("streetAddress");
        String postalCode = req.getParameter("postalCode");
        String areaCode = req.getParameter("areaCode");
        String phoneNumber = req.getParameter("phoneNumber");
        String extension = req.getParameter("extension");
        String mobilePhone = req.getParameter("mobilePhone");
        // 创建service对象
        HzyLocationService locationService = new HzyLocationServiceImpl();
        HzyLocation location = new HzyLocation();
        PrintWriter out = resp.getWriter();
        // 封装数据
        Map<String,Object> map = new HashMap<>();
        location.setReceiverName(receiverName);
        location.setProvince(province);
        location.setCity(city);
        location.setDistrict(district);
        location.setStreetAddress(streetAddress);
        location.setPostalCode(postalCode);
        location.setAreaCode(areaCode);
        location.setPhoneNumber(phoneNumber);
        location.setExtension(extension);
        location.setMobilePhone(mobilePhone);

        if (locationService.addLocation(location)){
            map.put("success",true);
            map.put("message","添加成功");
        }else {
            map.put("success",false);
            map.put("message","添加失败");
        }
        String json = JSON.toJSONString(map);
        out.print(json);
        out.close();
    }
}
